/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.alanis.gui;

import cl.alanis.business.ProductoBusiness;
import cl.alanis.dto.ComboDto;
import cl.alanis.dto.ProductoDto;
import cl.alanis.util.AlanisLog;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;


/**
 *
 * @author Pía
 */
public class MantenedorProductoGui extends javax.swing.JDialog {
    Logger logger=AlanisLog.obtenerLogger(MantenedorProductoGui.class);
    /**
     * Creates new form MantenedorProductoGui
     */
    public MantenedorProductoGui() {
        this.setModal(true);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCodProducto = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cmbTipoRepuesto = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        txtDescripcion = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtAplicacion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtAgno = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtOrigen = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtFabricante = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtValorCompra = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtValorVenta = new javax.swing.JTextField();
        btnIngresar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnBuscarCodProd = new javax.swing.JButton();
        txtCodProveedor = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        btnBuscarCodProv = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        chkActivo = new javax.swing.JCheckBox();
        lblError = new javax.swing.JLabel();
        btnActualizar = new javax.swing.JButton();
        cmbMarca = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Mantenedor Producto");
        setName("Mantenedor Producto");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("- INGRESO DE PRODUCTO NUEVO -");

        jLabel2.setText("Código Producto");

        jLabel3.setText("Marca");

        jLabel4.setText("Tipo Repuesto");

        cmbTipoRepuesto.setSelectedItem(cmbTipoRepuesto);

        jLabel5.setText("Descripción");

        jLabel6.setText("Aplicación");

        jLabel7.setText("Año Auto");

        jLabel9.setText("Origen Repuesto");

        jLabel10.setText("Fabricante");

        jLabel11.setText("Valor Compra $");

        jLabel12.setText("Valor Venta $");

        btnIngresar.setText("Ingresar");
        btnIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        btnBuscarCodProd.setText("Buscar");
        btnBuscarCodProd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarCodProdActionPerformed(evt);
            }
        });

        jLabel13.setText("Código Proveedor");

        btnBuscarCodProv.setText("Buscar");
        btnBuscarCodProv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarCodProvActionPerformed(evt);
            }
        });

        jLabel14.setText("Estado");

        chkActivo.setText("Activo");

        lblError.setForeground(new java.awt.Color(255, 51, 51));
        lblError.setText("---");

        btnActualizar.setText("Actualizar");
        btnActualizar.setEnabled(false);
        btnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGap(254, 254, 254)
                .addComponent(jLabel1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12)
                            .addComponent(jLabel14))
                        .addGap(41, 41, 41)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(chkActivo)
                                .addGap(72, 72, 72)
                                .addComponent(lblError))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtValorCompra)
                                .addComponent(txtValorVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnActualizar)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5))
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtDescripcion)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtOrigen)
                                .addComponent(txtAplicacion, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE)
                                .addComponent(txtFabricante, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtAgno)))
                        .addContainerGap(106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmbTipoRepuesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtCodProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnBuscarCodProd)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel13)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtCodProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(29, 29, 29)
                                        .addComponent(btnBuscarCodProv)))
                                .addGap(42, 42, 42))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmbMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCodProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscarCodProd)
                    .addComponent(jLabel13)
                    .addComponent(btnBuscarCodProv)
                    .addComponent(txtCodProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cmbTipoRepuesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtAplicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtAgno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtOrigen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtFabricante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtValorCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(txtValorVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(chkActivo)
                    .addComponent(lblError))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnIngresar)
                    .addComponent(btnCancelar)
                    .addComponent(btnLimpiar)
                    .addComponent(btnActualizar))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        ProductoBusiness business= new ProductoBusiness();
        
        try {
            List lista=lista=new ArrayList();
            lista=business.obtenerMarcas();            
            ComboDto opcionDefecto=new ComboDto();
            opcionDefecto.setCodigo(-1);
            opcionDefecto.setDescripcion("Seleccione");
            cmbMarca.addItem(opcionDefecto);
            Iterator iterador=lista.iterator();
            while(iterador.hasNext()){
                ComboDto opcion=new ComboDto();
                opcion=(ComboDto) iterador.next();
                cmbMarca.addItem(opcion);
            }            
            lista=new ArrayList();
            lista=business.obtenerTipoRepuestos();
            iterador=lista.iterator();
            
            cmbTipoRepuesto.addItem(opcionDefecto);
            while(iterador.hasNext()){
                ComboDto opcion=new ComboDto();
                opcion=(ComboDto) iterador.next();
                cmbTipoRepuesto.addItem(opcion);
            }
        } catch (Exception ex) {
            logger.error("Error:"+ex.getMessage());
            lblError.setText(ex.getMessage());
        }
    }//GEN-LAST:event_formWindowOpened

    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed
        ProductoBusiness business=new ProductoBusiness();
        boolean sw=false;
        String errores="";
        if("".equals(txtCodProducto.getText())){
            errores+="Debe ingresar código de producto\n";
            sw=true;
        }
        if("".equals(txtCodProveedor.getText())){
             errores+="Debe ingresar el codigo de producto en el proveedor\n";
             sw=true;
        }
        int opcionCmb=cmbMarca.getSelectedIndex();
        if(cmbMarca.getSelectedIndex()==0){
            errores+="Debe seleccionar una marca\n";
            sw=true;
            
        }
        if(cmbTipoRepuesto.getSelectedIndex()==0){
            errores+="Debe seleccionar un tipo de repuesto\n";
            sw=true;
        }
        if("".equals(txtDescripcion.getText())){
            errores+="Debe ingresar una descripcion del producto\n";
            sw=true;
        }    
        if("".equals(txtAplicacion.getText())){
            errores+="Debe ingresar la aplicacion de este producto\n";
            sw=true;
        }
        if("".equals(txtAgno.getText())){
            errores+="Debe ingresar el año del auto en que aplica este producto\n";
            sw=true;
        }
        if("".equals(txtOrigen.getText())){
            errores+="Debe ingresar el origen del producto\n";
            sw=true;
        }
        if("".equals(txtFabricante.getText())){
            errores+="Debe ingresar el fabricante\n";
            sw=true;
        }
        if("".equals(txtValorCompra.getText())){
            errores+="Debe ingresar el valor de compra\n";
            sw=true;
        }
        if("".equals(txtValorVenta.getText())){
            errores+="Debe ingresar el valor de venta\n";
            sw=true;
        }
        if(sw){
            JOptionPane.showMessageDialog(null, errores, "Mantenedor de Productos", JOptionPane.PLAIN_MESSAGE);
        }
        else{
            ProductoDto producto=new ProductoDto();
            producto.setCodProducto(txtCodProducto.getText());
            producto.setCodProveedor(txtCodProveedor.getText());
            ComboDto opcion=(ComboDto) cmbMarca.getSelectedItem();
            producto.setCodMarca(opcion.getCodigo());
            opcion=(ComboDto) cmbTipoRepuesto.getSelectedItem();
            producto.setCodTipoRepuesto(opcion.getCodigo());
            producto.setDescripcionProducto(txtDescripcion.getText());
            producto.setAplicacion(txtAplicacion.getText());
            producto.setAgnoAuto(txtAgno.getText());
            producto.setOrigenRepuesto(txtOrigen.getText());
            producto.setFabricante(txtFabricante.getText());
            producto.setValorCompra(Integer.valueOf(txtValorCompra.getText()));
            producto.setValorVenta(Integer.valueOf(txtValorVenta.getText()));
            producto.setActivo(chkActivo.isSelected());
            try {
                business.actualizarProducto(producto);
                lblError.setText("Producto actualizado correctamente");
                
            } catch (Exception ex) {
                logger.error("Error:"+ex.getMessage());
                lblError.setText(ex.getMessage());
            }
        }
    }//GEN-LAST:event_btnActualizarActionPerformed

    private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresarActionPerformed
        ProductoBusiness business=new ProductoBusiness();
        boolean sw=false;
        String errores="";
        if("".equals(txtCodProducto.getText())){
            errores+="Debe ingresar código de producto\n";
            sw=true;
        }
        if("".equals(txtCodProveedor.getText())){
             errores+="Debe ingresar el codigo de producto en el proveedor\n";
             sw=true;
        }
        int opcionCmb=cmbMarca.getSelectedIndex();
        if(cmbMarca.getSelectedIndex()==0){
            errores+="Debe seleccionar una marca\n";
            sw=true;
            
        }
        if(cmbTipoRepuesto.getSelectedIndex()==0){
            errores+="Debe seleccionar un tipo de repuesto\n";
            sw=true;
        }
        if("".equals(txtDescripcion.getText())){
            errores+="Debe ingresar una descripcion del producto\n";
            sw=true;
        }    
        if("".equals(txtAplicacion.getText())){
            errores+="Debe ingresar la aplicacion de este producto\n";
            sw=true;
        }
        if("".equals(txtAgno.getText())){
            errores+="Debe ingresar el año del auto en que aplica este producto\n";
            sw=true;
        }
        if("".equals(txtOrigen.getText())){
            errores+="Debe ingresar el origen del producto\n";
            sw=true;
        }
        if("".equals(txtFabricante.getText())){
            errores+="Debe ingresar el fabricante\n";
            sw=true;
        }
        if("".equals(txtValorCompra.getText())){
            errores+="Debe ingresar el valor de compra\n";
            sw=true;
        }
        if("".equals(txtValorVenta.getText())){
            errores+="Debe ingresar el valor de venta\n";
            sw=true;
        }
        if(sw){
            JOptionPane.showMessageDialog(null, errores, "Mantenedor de Productos", JOptionPane.PLAIN_MESSAGE);
        }
        else{
            ProductoDto producto=new ProductoDto();
            producto.setCodProducto(txtCodProducto.getText());
            producto.setCodProveedor(txtCodProveedor.getText());
            ComboDto opcion=(ComboDto) cmbMarca.getSelectedItem();
            producto.setCodMarca(opcion.getCodigo());
            opcion=(ComboDto) cmbTipoRepuesto.getSelectedItem();
            producto.setCodTipoRepuesto(opcion.getCodigo());
            producto.setDescripcionProducto(txtDescripcion.getText());
            producto.setAplicacion(txtAplicacion.getText());
            producto.setAgnoAuto(txtAgno.getText());
            producto.setOrigenRepuesto(txtOrigen.getText());
            producto.setFabricante(txtFabricante.getText());
            producto.setValorCompra(Integer.valueOf(txtValorCompra.getText()));
            producto.setValorVenta(Integer.valueOf(txtValorVenta.getText()));
            producto.setActivo(chkActivo.isSelected());
            
            try {
                if(business.ingresarProducto(producto)){
                    logger.info("Producto insertado corectamente");
                    lblError.setText("Producto insertado correctamente");
                }
            } catch (Exception ex) {
                logger.error("Error:"+ex.getMessage());
                lblError.setText(ex.getMessage());
            }
        }
    }//GEN-LAST:event_btnIngresarActionPerformed

    private void btnBuscarCodProdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarCodProdActionPerformed
        ProductoBusiness business=new ProductoBusiness();
        String errores="";
        boolean sw=false;
        if("".equals(txtCodProducto.getText())){
            errores+="Debe ingresar código de producto\n";
            sw=true;
        }
        if(sw)
            JOptionPane.showMessageDialog(null, errores, "Mantenedor de Productos", JOptionPane.PLAIN_MESSAGE);
        else{
             ProductoDto producto=new ProductoDto();
             producto.setCodProducto(txtCodProducto.getText());
            try {
                producto=business.obtenerProducto(producto);
                
            } catch (Exception ex) {
                logger.error("Error:"+ex.getMessage());
                lblError.setText(ex.getMessage());
            }
            txtAgno.setText(producto.getAgnoAuto());
            txtAplicacion.setText(producto.getAplicacion());
            txtDescripcion.setText(producto.getDescripcionProducto());
            txtFabricante.setText(producto.getFabricante());
            txtOrigen.setText(producto.getOrigenRepuesto());
            txtValorCompra.setText(String.valueOf(producto.getValorCompra()));
            txtValorVenta.setText(String.valueOf(producto.getValorVenta()));
            chkActivo.setSelected(producto.getActivo());
            ComboDto opcion=new ComboDto();
            opcion.setCodigo(producto.getCodMarca());
            opcion.setDescripcion(producto.getMarca());
            cmbMarca.setSelectedItem(opcion);
            opcion.setCodigo(producto.getCodTipoRepuesto());
            opcion.setDescripcion(producto.getTipoRepuesto());
            cmbTipoRepuesto.setSelectedItem(opcion);
            txtCodProveedor.setText(producto.getCodProveedor());
            btnActualizar.setEnabled(true);
        }
    }//GEN-LAST:event_btnBuscarCodProdActionPerformed

    private void btnBuscarCodProvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarCodProvActionPerformed
        ProductoBusiness business=new ProductoBusiness();
        String errores="";
        boolean sw=false;
        if("".equals(txtCodProveedor.getText())){
             errores+="Debe ingresar el codigo de producto en el proveedor\n";
             sw=true;
        }
        if(sw)
            JOptionPane.showMessageDialog(null, errores, "Mantenedor de Productos", JOptionPane.PLAIN_MESSAGE);
        else{
             ProductoDto producto=new ProductoDto();
             producto.setCodProveedor(txtCodProveedor.getText());
            try {
                producto=business.obtenerProducto(producto);
                txtAgno.setText(producto.getAgnoAuto());
                txtAplicacion.setText(producto.getAplicacion());
                txtDescripcion.setText(producto.getDescripcionProducto());
                txtFabricante.setText(producto.getFabricante());
                txtOrigen.setText(producto.getOrigenRepuesto());
                txtValorCompra.setText(String.valueOf(producto.getValorCompra()));
                txtValorVenta.setText(String.valueOf(producto.getValorVenta()));
                chkActivo.setSelected(producto.getActivo());
                ComboDto opcion=new ComboDto();
                opcion.setCodigo(producto.getCodMarca());
                opcion.setDescripcion(producto.getMarca());
                cmbMarca.setSelectedItem(opcion);
                opcion.setCodigo(producto.getCodTipoRepuesto());
                opcion.setDescripcion(producto.getTipoRepuesto());
                cmbTipoRepuesto.setSelectedItem(opcion);
                txtCodProducto.setText(producto.getCodProducto());
                btnActualizar.setEnabled(true);
            } catch (Exception ex) {
                logger.error("Error:"+ex.getMessage());
                lblError.setText(ex.getMessage());
            }
            
        }
    }//GEN-LAST:event_btnBuscarCodProvActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
            txtAgno.setText("");
            txtAplicacion.setText("");
            txtDescripcion.setText("");
            txtFabricante.setText("");
            txtOrigen.setText("");
            txtValorCompra.setText("");
            txtValorVenta.setText("");
            chkActivo.setSelected(false);
            ComboDto opcion=new ComboDto();
            opcion.setCodigo(-1);
            opcion.setDescripcion("Seleccione");
            cmbMarca.setSelectedItem(opcion);            
            cmbTipoRepuesto.setSelectedItem(opcion);
            txtCodProveedor.setText("");
            txtCodProducto.setText("");
            btnActualizar.setEnabled(false);
            lblError.setText("");
    }//GEN-LAST:event_btnLimpiarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MantenedorProductoGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MantenedorProductoGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MantenedorProductoGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MantenedorProductoGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MantenedorProductoGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualizar;
    private javax.swing.JButton btnBuscarCodProd;
    private javax.swing.JButton btnBuscarCodProv;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnIngresar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JCheckBox chkActivo;
    private javax.swing.JComboBox cmbMarca;
    private javax.swing.JComboBox cmbTipoRepuesto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblError;
    private javax.swing.JTextField txtAgno;
    private javax.swing.JTextField txtAplicacion;
    private javax.swing.JTextField txtCodProducto;
    private javax.swing.JTextField txtCodProveedor;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtFabricante;
    private javax.swing.JTextField txtOrigen;
    private javax.swing.JTextField txtValorCompra;
    private javax.swing.JTextField txtValorVenta;
    // End of variables declaration//GEN-END:variables
}
